package com.dingding.api.controller.pay;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dingding.api.controller.base.PayBaseController;
import com.dingding.bean.api.wechat.WechatPayResult;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.XMLParse;
import com.dingding.order.service.IUserOrderFacade;

/**
 * 
 * @ClassName: TaxiPayNotifyController 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author CHEHAUNBO 
 * @date 2016年7月6日 下午3:35:59
 * 
 */
@Scope("prototype")
@Controller("taxiPayNotifyController")
@RequestMapping("/authorapi/taxipay")
public class TaxiPayNotifyController extends PayBaseController {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 用户订单操作service
	 */
	@Resource(name = "userOrderFacade")
	private IUserOrderFacade userOrderFacade;

	/**
	 * 
	 * 支付宝回调方法
	 * 
	 * @Title: alipayNotify 
	 * @author CHEHAUNBO
	 * @since V2.3.0
	 * 
	 */
	@Override
	@RequestMapping(value = "/v1/alipayNotify", method = RequestMethod.POST)
	public void alipayNotify(HttpServletRequest request, HttpServletResponse response) {

		// 交易状态
		String trade_status = request.getParameter("trade_status");
		// 订单ID
		String out_trade_no = request.getParameter("out_trade_no");

		// 判断支付宝回调服务器时，传入的参数是否为空，如果为空，则返回null
		if (ToolUtil.isBlank(trade_status) || ToolUtil.isBlank(out_trade_no)) {
			log.error("支付宝回调服务器时，传入的参数为空！！！");
			return;
		}

		// 判断支付宝交易是否成功
		if ("TRADE_SUCCESS".equals(trade_status)) {
			// 更改订单状态
			if (userOrderFacade.taxiOrderPayNotify(out_trade_no)) {
				super.returnSuccess(response);
			}
		} else if ("WAIT_BUYER_PAY".equals(trade_status)) {// 判断是否为交易创建
			super.returnSuccess(response);
		} else if ("TRADE_FINISHED".equals(trade_status)) {// 判断是否为交易成功
			super.returnSuccess(response);
		} else if ("TRADE_CLOSED".equals(trade_status)) {// 判断是否为交易关闭
			super.returnSuccess(response);
		}
		return;
	}

	/**
	 * 
	 * 微信支付回调方法
	 * 
	 * @Title: alipayNotify 
	 * @author CHEHAUNBO
	 * @since V2.3.0
	 * 
	 */
	@Override
	@RequestMapping(value = "/v1/weChatPayNotify", method = RequestMethod.POST)
	public void weChatNotify(HttpServletRequest request, HttpServletResponse response) {

		// 读取接收到微信服务器传来的xml信息
		String requestXml = null;
		try {
			requestXml = ToolUtil.readStreamToText(request.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

		log.info("微信传来的支付结果如下:" + requestXml);

		// 把接受到微信服务器传来的xml信息转化为实体
		WechatPayResult wechatPayResult = XMLParse.toObject(requestXml, WechatPayResult.class);

		if (wechatPayResult == null) {
			responseFailToWechat(response);
			return;
		}

		// 当服务器成功接收到通知并校验成功，返回给微信ok
		if (userOrderFacade.taxiOrderPayNotify(wechatPayResult.getOut_trade_no())) {
			responseOkToWechat(response);
		} else {
			responseFailToWechat(response);
		}

		return;

	}

}
